/**
 *  列表页
 *
 * 作者 Lujiachen
 * 日期 2024-08-12 15:42:05
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-hazardReportConfig'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#hazardReportConfig-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        tableIns;

    form.verify(validate);
    form.render();

    initFormValue();
    function initFormValue() {
        if (hazardReportConfig) {
            form.val("hazardReportConfig-form", {
                "reportConfigId": hazardReportConfig.reportConfigId,
                "tenantId": hazardReportConfig.tenantId,
                "reportNumber": hazardReportConfig.reportNumber,
            });

        }
    }



    function reloadPage() {
        var tenantId=$tenantSelect.val();
        febs.get(ctx + 'hazardReportConfig/search/' + tenantId, null, function (data) {
            var newHazardReportConfig = "";
            if(data.code==200&&data.data.hazardReportConfig){
                newHazardReportConfig = data.data.hazardReportConfig;
            }
            form.val("hazardReportConfig-form", {
                "reportConfigId": newHazardReportConfig.reportConfigId,
                "tenantId": newHazardReportConfig.tenantId,
                "reportNumber": newHazardReportConfig.reportNumber,
            });
        })
    }

    $query.on('click', function () {
        reloadPage();
    });

    form.on('submit(hazardReportConfig-form-submit)', function (data) {
        if(data.field.tenantId==null || data.field.tenantId ==''){
            data.field.tenantId = $tenantSelect.val();
        }
        febs.modal.confirm('保存提示', '是否保存隐患上报设置？', function () {
            if (data.field.reportConfigId && data.field.reportConfigId.length > 0) {
                febs.postArray(ctx + 'hazardReportConfig/update', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                });
            } else {
                febs.postArray(ctx + 'hazardReportConfig', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                    reloadPage();
                });
            }
        });

        return false;
    });




});